package com.lct.simpleforecast.ui.activity

import android.support.v7.widget.LinearLayoutManager
import com.lct.simpleforecast.AppConstant
import com.lct.simpleforecast.R
import com.lct.simpleforecast.bean.CityBean
import com.lct.simpleforecast.delegates.DelegatesExt
import com.lct.simpleforecast.extensions.afterTextChanged
import com.lct.simpleforecast.extensions.hideKeyboard
import com.lct.simpleforecast.mvp.presenter.SettingPresenter
import com.lct.simpleforecast.mvp.view.ISettingView
import com.lct.simpleforecast.ui.activity.base.BaseActivity
import com.lct.simpleforecast.ui.adapter.CityAdapter
import kotlinx.android.synthetic.main.activity_setting.*
import kotlin.properties.Delegates

class SettingActivity : BaseActivity(), ISettingView {
    private val presenter = SettingPresenter()
    private val cityList: MutableList<CityBean.HeWeather.Basic> by lazy {
        arrayListOf<CityBean.HeWeather.Basic>()
    }
    private var adapter: CityAdapter by Delegates.notNull()
    //位置，默认为空
    private var location: String by DelegatesExt.preference(AppConstant.SP_KEY_LOCATION, "")

    override fun onDestroy() {
        super.onDestroy()
        presenter.detachView()
    }

    /**
     * 设置activity的布局
     */
    override fun setLayoutResId(): Int {
        return R.layout.activity_setting
    }

    /**
     * 初始化数据
     */
    override fun initData() {
        //toolbar
        toolbar?.title = resources.getString(R.string.setting)
        enableHomeAsUp()
        //adapter
        rvCity.layoutManager = LinearLayoutManager(this)
        adapter = CityAdapter(R.layout.rv_city_item, cityList) {
            location = it.location
            finish()
        }
        rvCity.adapter = adapter
        //presenter
        presenter.attachView(this)
    }

    override fun initListener() {
        super.initListener()
        etSearch.afterTextChanged {
            presenter.getCityData(it)
        }
        rvCity.setOnTouchListener { v, _ ->
            v.hideKeyboard()
        }
    }

    /**
     * 网络不可用
     *
     * @param mark 标记哪种情况下发生的网络不可用,默认为空
     * @param isRefresh 是否是刷新的回调（true是刷新的回调，false第一次加载的回调）
     */
    override fun onNetWorkError(mark: String, isRefresh: Boolean) {
        searchLoadingView.loadingFailText = resources.getString(R.string.network_error)
        searchLoadingView.showLoadingFail()
    }

    /**
     * 数据加载成功
     *
     * @param t 实体类对象
     * @param isRefresh 是否是刷新的回调（true是刷新的回调，false第一次加载的回调）
     */
    override fun <T> onLoadSuccess(t: T, isRefresh: Boolean) {
        with(t as CityBean) {
            cityList.clear()
            cityList.addAll(cityList())
            adapter.notifyDataSetChanged()
        }
    }

    /**
     * 数据加载失败
     *
     * @param errorMsg 错误信息
     * @param isRefresh 是否是刷新的回调（true是刷新的回调，false第一次加载的回调）
     */
    override fun onLoadFailure(errorMsg: String, isRefresh: Boolean) {
        searchLoadingView.loadingFailText = errorMsg
        searchLoadingView.showLoadingFail()
    }

    /**
     * 显示加载loading
     */
    override fun showLoading() {
        searchLoadingView.showLoading()
    }

    /**
     * 隐藏加载loading
     */
    override fun hideLoading() {
        searchLoadingView.hideLoading()
    }

}
